package com.kuqi.ViewComponent.SlideShow;

import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import com.kuqi.entitiy.BannerItem;

import java.util.List;

/**
 *  轮播图图像处理
 */
public class HomePageADAdapter extends PagerAdapter {


    // 轮播banner的数据
    private List<BannerItem> adList;

    // 滑动的图片对象集合
    private List<ImageView> imageViews;

    // 构造函数
    public HomePageADAdapter(List<BannerItem> adList, List<ImageView> imageViews) {
        this.adList = adList;
        this.imageViews = imageViews;
    }

    @Override
    public int getCount() {
        return adList.size();
    }

    @NonNull
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        ImageView iv = imageViews.get(position);
        container.addView(iv);
        // 在这个方法里面设置图片的点击事件
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                    // 处理跳转逻辑
//                    BannerItem item = (BannerItem) adList.get(position);
//                    Intent i = new Intent();
//                    //TODO 因为此处存在异常,所以先注释掉
//                    //i.setClass(getActivity(), BiliWebviewActivity.class);
//                    i.putExtra("bannerLink", item.getLink());
//                    startActivity(i);
//                    // 动画过渡
//                    getActivity().overridePendingTransition(R.anim.push_left_in,
//                            R.anim.push_no);
                Log.e("position", "触发点击事件,点击了:" + position);
            }
        });
        return iv;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View)object);
        //记得注释super 因为它会在super中弹出未实现那个exception
        //super.destroyItem(container, position, object);
    }

    @Override
    public boolean isViewFromObject(@NonNull View arg0, @NonNull Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {

    }

    @Override
    public Parcelable saveState() {
        return null;
    }
}

